import app from "../../../app";
import { cfg_effectKey, _getEffect } from "../../../intoSkill/cfg_effect";
import { BuffEffect, Effect_tag, EffectCondition, group, Sk_minType, SK_type } from "../../../shared/interface";
import { logger } from "../../server/logger";
import battlefield from "../battle/battlefield";
import { baseBody } from "../body/baseBody";
import { player } from "../body/player";
import { SKILL, SK_data, SK_data_db } from "./SK_active";
export interface Effect_Cfg_Input extends SK_data_db {
    effect: {
        tag: Effect_tag,
        condition: EffectCondition;
        effect_key: cfg_effectKey
    }[],
    minType?:Sk_minType
    type: SK_type,
    name: string,
    desc: string,
    useUp: string
}
class SK_func {
    skTplMap: Map<string, SK_data> = new Map();
    constructor() {

    }
    _addSkillTpl(data: Effect_Cfg_Input) {
        if (this.skTplMap.has(data.name)) {
            logger.error('技能名称已重复||', data.name);
            debugger;
        }
        let _effectArry: any[] = [];
        for (let i = 0; i < data.effect.length; i++) {
            const e = data.effect[i];
            let _effect = _getEffect(e.effect_key);
            _effectArry.push({ ..._effect, ...e })
        }
        data.effect = _effectArry;
        this.skTplMap.set(data.name, data as SK_data);
        logger.debug(data.name, '||技能已注册')
    }
    /**
     * 单位主动释放技能
     * @param battle 
     * @param body 
     * @returns 
     */
    skill_use(battle: battlefield, body: baseBody) {
        const arry = this.skill_filter(battle, body);
        if (arry.length == 0) {
            console.log(`${body.name}没有可释放的技能`);
            return;
        }
        if (body.getBuffVal(BuffEffect.沉默)) {
            // TODO:完善沉默
            let _buff_item = body.getBuff_effect(BuffEffect.沉默)
            logger.debug('用户已沉默,无法使用技能剩余时间:', _buff_item?.surplusTime_s());
        }

        let select = this.skill_select(arry, body);
        if(!select){
            return;
        }
        select.checkUseUp(true);
        let allBody = battle.getBody_all();
        select.use(allBody)
        return select;
    }
    /**
     * 过滤获得可释放的技能
     */
    private skill_filter(battle: battlefield, body: baseBody) {
        let allSk = body.getAllSkills();
        let resSk:SKILL[] = [];
        // 判定技能消耗
        let 沉默 = body.getBuffVal(BuffEffect.沉默)
        for (let i = 0; i < allSk.length; i++) {
            const e = allSk[i];
            if(!e.checkUseCd()){
                continue;
            }
            if(e.checkUseUp()){
            }
            if(沉默 && e.minType != Sk_minType.普通攻击){
                continue;
            }
            resSk.push(e);
        }
        return resSk
    }
    /**
     * 选择技能
     */
    private skill_select(arry: SKILL[], body: baseBody) {
        let select = arry.find((item) => { return item.rename == body.getSelectSkill() })
        if (select) {
            return select;
        }

        let _sks = arry.filter((item) => { return item.type == SK_type.主动技能 });
        let _idx = app.random(0, _sks.length - 1)
        return _sks[_idx];
    }
    /**
     * 根据输入技能名称 评分
     * 输出一个技能
     */
    createSkill(id: string, score: number,rename?:string): SKILL {
        let tpl = this.getSkCfg_name(id) as SK_data;
        if (!tpl) {
            logger.error('创建技能出错-id:', id);
            debugger;
        }
        for (let index = 0; index < tpl.effect.length; index++) {
            const element = tpl.effect[index];
            element.create(score);
        }
        let sk = new SKILL(tpl);
        sk.score = score
        if(rename){
            sk.rename = rename
        }
        return sk;
    }
    /**
     * 读取指定名称技能
     */
    getSkCfg_name(name: string) {
        let originalSkill = this.skTplMap.get(name);

        return app.deepCopy(originalSkill) as SK_data;
    }
}
export default new SK_func();